package com.weibo.blacklist.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.weibo.blacklist.BlackList;
import com.weibo.blacklist.dao.BlackListDao;
import com.weibo.common.CommonUtil;
import com.weibo.common.DB;
import com.weibo.sql.CommonParm;
import com.weibo.sql.SqlFile;

public class BlackListImp implements BlackListDao {
	
	private static Logger logger = Logger.getLogger(BlackList.class);
	
	@Override
	public String addBlack(BlackList blackList) {
		// TODO Auto-generated method stub
		ResultSet rs = DB.plsqlExec_res(CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_ISEXIST,blackList.getUser_id(), blackList.getHost_id()));
		try {
			if(rs.next()){
				return CommonParm.BLACKLIST_EXIST;
			}else{
				DB.plsqlExec_res(CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_ADD, blackList.getUser_id(),blackList.getHost_id(),blackList.getBla_other()));
				return CommonParm.BLACKLIST_SUCCESS;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e);
			return CommonParm.ERROR;
		}
	}

	@Override
	public String deleteBlaceList(String bla_id) {
		// TODO Auto-generated method stub
		if(DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_DELETE, bla_id))){
			return CommonParm.BLACKLIST_DELETE_SUCCESS;
		}else{
			return CommonParm.BALCKLIST_DELETE_ERROR;
		}
	}

	@Override
	public List selectBalckList() {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(SqlFile.BLACKLIST_SEARCH);
	}

	@Override
	public List selectBlackList(int currentPage,String user_id) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_SEARCH, user_id), currentPage));
	}

}
